<?xml version="1.0" encoding="utf-8"?>
<!--
	Copyright (C) 2010 Ethan Gruber
	EADitor: http://code.google.com/p/eaditor/
	Apache License 2.0: http://code.google.com/p/eaditor/    
-->
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ev="http://www.w3.org/2001/xml-events"
	xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:widget="http://orbeon.org/oxf/xml/widget" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
	xmlns:exist="http://exist.sourceforge.net/NS/exist" xmlns:ead="urn:isbn:1-931666-22-9">
	<xhtml:head>
		<xhtml:title>EADitor: Edit Agency Codes</xhtml:title>
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/eaditor/css/grids-min.css"/>
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/eaditor/css/reset-fonts-grids.css"/>
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/eaditor/css/base-min.css"/>
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/eaditor/css/fonts-min.css"/>

		<!-- EADitor styling -->
		<xhtml:link rel="stylesheet" href="/apps/eaditor/css/style.css" type="text/css"/>
		<xhtml:link rel="stylesheet" href="/apps/eaditor/css/themes/smoothness.css"/>

		<xhtml:script type="text/javascript" src="/apps/eaditor/javascript/jquery-1.4.2.min.js"/>
		<xhtml:script type="text/javascript" src="/apps/eaditor/javascript/menu.js"/>

		<xforms:model schema="http://www.loc.gov/ead/ead.xsd">
			<!-- exist URL is stored in an XML file -->
			<xforms:instance id="exist-url">
				<xi:include href="exist-url.xml"/>
			</xforms:instance>

			<xforms:instance id="instance-templates">
				<templates xmlns=""/>
			</xforms:instance>

			<xforms:instance id="agencycode-template">
				<agencycode value="" xmlns=""/>
			</xforms:instance>
			
			<xforms:instance id="container-template">
				<container value="" xmlns=""/>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="temp-instance">
				<temp/>
			</xforms:instance>

			<!-- eXist XQuery for localized agencycodes -->
			<xforms:instance id="agencycodes-instance">
				<exist:query xmlns="">
					<exist:text>&lt;report> { for $foo in collection() return $foo//*[local-name()='eadid'] } &lt;/report></exist:text>
				</exist:query>
			</xforms:instance>

			<xforms:instance id="control-instance">
				<control xmlns="">
					<term>eadid</term>
					<resource>eaditor/guides/</resource>
					<query/>
					<error/>
					<formatted-result/>
				</control>
			</xforms:instance>

			<xforms:instance id="response-instance">
				<exist:result/>
			</xforms:instance>

			<xforms:bind nodeset="instance('instance-templates')">
				<xforms:bind nodeset="agencycodes">
					<xforms:bind nodeset="agencycode/@value" required="true()" type="ead:data.repositorycode"/>
					<xforms:bind nodeset="agencycode" required="true()"/>
				</xforms:bind>
				<xforms:bind nodeset="containertypes">
					<xforms:bind nodeset="container/@value" required="true()" type="xs:NMTOKEN"/>
					<xforms:bind nodeset="container" required="true()"/>
				</xforms:bind>
			</xforms:bind>

			<xforms:bind id="agencycodes-tab" nodeset="instance('instance-templates')/agencycodes"/>
			<xforms:bind id="containertypes-tab" nodeset="instance('instance-templates')/containertypes"/>

			<!-- submit query to eXist -->
			<xforms:submission id="agencycodes-submission" ref="instance('agencycodes-instance')" action="{instance('exist-url')}{instance('control-instance')/resource}" method="post"
				replace="instance" instance="response-instance">
				<xforms:insert nodeset="instance('temp-instance')" ev:event="xforms-submit-done"
					origin="xxforms:call-xpl('oxf:/apps/eaditor/xpl/get-agencycodes.xpl', 'data', instance('response-instance'), 'data')"/>
				<xforms:action xxforms:iterate="instance('temp-instance')/agencycode" ev:event="xforms-submit-done">
					<xforms:insert context="instance('instance-templates')" nodeset="agencycode[position() = last()]" origin="instance('agencycode-template')"/>
					<xforms:setvalue ref="instance('instance-templates')/agencycode[last()]" value="context()"/>
					<xforms:setvalue ref="instance('instance-templates')/agencycode[last()]/@value" value="context()"/>
				</xforms:action>
			</xforms:submission>

			<!-- error report -->
			<xforms:setvalue ev:event="xforms-submit-error" ref="instance('control-instance')/error" value="event('response-body')"/>

			<!-- Submission to get the document -->
			<xforms:submission id="load-instance-templates" serialization="none" method="get" action="{instance('exist-url')}eaditor/instance-templates.xml" replace="instance" xxforms:username="admin"
				xxforms:password="" instance="instance-templates"/>

			<!-- Submission to save the document -->
			<xforms:submission id="save-instance-templates" ref="instance('instance-templates')" action="{instance('exist-url')}eaditor/instance-templates.xml" method="put" replace="none"
				xxforms:username="admin" xxforms:password="">
				<xforms:message ev:event="xforms-submit-error" level="modal">An error occurred while saving to eXist.</xforms:message>
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">Templates saved.</xforms:setvalue>
			</xforms:submission>

			<xforms:send ev:event="xforms-model-construct-done" submission="load-instance-templates"/>
		</xforms:model>
	</xhtml:head>
	<xhtml:body class="yui-skin-sam">
		<xhtml:div id="doc4">
			<!-- header -->
			<xxforms:variable name="display_path">../../../</xxforms:variable>
			<xi:include href="header-admin.xml"/>

			<xhtml:div id="bd">
				<xforms:group ref="instance('status')/text()">
					<xhtml:div class="success">
						<xforms:output ref="instance('status')"/>
					</xhtml:div>
				</xforms:group>
				<p>
					<a href="../../templates/">&lt; Return</a>
				</p>
				<!-- This table contains the main actions for the page  -->
				<xhtml:div class="submission">
					<xforms:submit submission="save-instance-templates" appearance="minimal">
						<xforms:label><xhtml:img src="/apps/eaditor/images/save.gif" alt="Save"/> Save</xforms:label>
					</xforms:submit>
					<xforms:submit submission="load-instance-templates" appearance="minimal">
						<xforms:label><xhtml:img src="/apps/eaditor/images/recycle-green.png" alt="Revert"/>Load</xforms:label>
					</xforms:submit>
				</xhtml:div>
				<xhtml:h1>Edit Instance Templates</xhtml:h1>
				<fr:tabview>
					<fr:tab bind="agencycodes-tab">
						<fr:label>Agency Codes</fr:label>
						<xhtml:h2>Agency Codes</xhtml:h2>
						<xforms:trigger appearance="minimal">
							<xforms:label>
								<xhtml:img src="/apps/eaditor/images/add.gif"/>New Agency</xforms:label>
							<xforms:insert ev:event="DOMActivate" context="." nodeset="./child::node()[last()]" origin="instance('agencycode-template')"/>
						</xforms:trigger>
						<xforms:trigger appearance="minimal">
							<xforms:label><xhtml:img src="/apps/eaditor/images/add.gif"/>Import Local Agencies</xforms:label>
							<xforms:action ev:event="DOMActivate">
								<xforms:send submission="agencycodes-submission"/>
							</xforms:action>
						</xforms:trigger>
						<xforms:repeat nodeset="agencycode">
							<xhtml:div class="pair_div">
								<xhtml:div>
									<xforms:input ref=".">
										<xforms:label>Agency</xforms:label>
										<xforms:alert>Required</xforms:alert>
									</xforms:input>
								</xhtml:div>
								<xhtml:div>
									<xforms:input ref="@value">
										<xforms:label>Code</xforms:label>
										<xforms:alert>The value is required and must meet the schema specification for the data.repositorycode type.</xforms:alert>
									</xforms:input>
								</xhtml:div>
								<xforms:group ref=".[count(//agencycode) &gt; 1]">
									<xforms:trigger appearance="minimal" style="float:right">
										<xforms:delete ev:event="DOMActivate" nodeset="."/>
										<xforms:label>
											<xhtml:img src="/apps/eaditor/images/remove.gif"/>
										</xforms:label>
									</xforms:trigger>
								</xforms:group>
							</xhtml:div>
						</xforms:repeat>
					</fr:tab>
					<fr:tab bind="containertypes-tab">
						<fr:label>Container Types</fr:label>
						<xhtml:h2>Container Types</xhtml:h2>
						<xforms:trigger appearance="minimal">
							<xforms:label>
								<xhtml:img src="/apps/eaditor/images/add.gif"/>New Agency</xforms:label>
							<xforms:insert ev:event="DOMActivate" context="." nodeset="./child::node()[last()]" origin="instance('container-template')"/>
						</xforms:trigger>
						<xforms:trigger appearance="minimal">
							<xforms:label><xhtml:img src="/apps/eaditor/images/add.gif"/>Import Local Container Types</xforms:label>
							<xforms:action ev:event="DOMActivate">
								<xforms:send submission="containertypes-submission"/>
							</xforms:action>
						</xforms:trigger>
						<xforms:repeat nodeset="container">
							<xhtml:div class="pair_div">
								<xhtml:div>
									<xforms:input ref=".">
										<xforms:label>Agency</xforms:label>
										<xforms:alert>Required</xforms:alert>
									</xforms:input>
								</xhtml:div>
								<xhtml:div>
									<xforms:input ref="@value">
										<xforms:label>Code</xforms:label>
										<xforms:alert>Required; must be xs:NMTOKEN type</xforms:alert>
									</xforms:input>
								</xhtml:div>
								<xforms:group ref=".[count(//container) &gt; 1]">
									<xforms:trigger appearance="minimal" style="float:right">
										<xforms:delete ev:event="DOMActivate" nodeset="."/>
										<xforms:label>
											<xhtml:img src="/apps/eaditor/images/remove.gif"/>
										</xforms:label>
									</xforms:trigger>
								</xforms:group>
							</xhtml:div>
						</xforms:repeat>
					</fr:tab>
				</fr:tabview>

				<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector"
					xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
			</xhtml:div>

			<!-- footer -->
			<xi:include href="xslt/footer.xml"/>
		</xhtml:div>
	</xhtml:body>

</xhtml:html>
